//
//  MainViewController.swift
//  Weibo1.0
//
//  Created by 黄礼潮 on 16/2/16.
//  Copyright © 2016年 黄礼潮. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //MARK -- 按钮点击事件
    //如果 “单纯”加上private 动态运行时无法正确的发送消息，导致崩溃
    // @objc（OC的）动态运行时能够正确的发送消息
   @objc private func clickComposeButton(){
    
     // print("点我了")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
       addChildViewControllers()
        
        //print(tabBar.subviews)//为空
        
       setUpcomposeButton()//按钮会被添加到最下面
        
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
      //  print(tabBar.subviews)//  button 在tabbar的下面  要出现 会将控制器的内部控件了哪家在出来
        //将按钮添加到前面
        
        tabBar.bringSubviewToFront(composeButton)
        
    }
    
    //懒加载 --button 控件
    
    private lazy var composeButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
}
//    = {
//
//        let  button = UIButton()
//        
//        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: .Normal)
//        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
//        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: .Normal)
//       button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: .Highlighted)
//        
//        //根据背景图片大小设置尺寸
//        
//        button.sizeToFit()
//    
//        return button
//    }()
//    
//}

    //MARK --设置界面
extension MainViewController {
    //设置撰写按钮
    
    private func setUpcomposeButton() {
    
        tabBar.addSubview(composeButton)
        
        //设置frame
        let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
    //这样  点击button  会透过
        
        //添加按钮点击事件
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
    
    }
    
    
   private func addChildViewControllers() {
        
        //设置按钮高亮的颜色
    
      
        
       // tabBar.tintColor = UIColor.orangeColor() 在appdelegate中设置了全局外观
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
       addChildViewController(UIViewController())
    
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    
    
    }
    
   private func addChildViewController(vc:UIViewController ,title:String,imageName:String) {
        
        vc.title = title //由内到外  设置title
        
        //vc.tabBarItem.title = "首页"
        
        vc.tabBarItem.image = UIImage(named:imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }

    
    
  }

    

